package com.itdianzi.controller.invoice;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.cargo.ShippingOrderExample;
import com.itdianzi.domain.invoice.Invoice;
import com.itdianzi.domain.invoice.InvoiceExample;
import com.itdianzi.export_common.common.utils.DownloadUtil;
import com.itdianzi.service.cargo.PackingService;
import com.itdianzi.service.cargo.ShippingOrderService;
import com.itdianzi.service.invoice.InvoiceService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/invoice")
public class InvoiceController extends BaseController {

    @Reference
    private ShippingOrderService shippingOrderService;//委托单
    @Reference
    private InvoiceService invoiceService;//发票单
    @Reference
    private PackingService packingService;//装箱单 -> 多个报运单exportIds -> 多个合同单下总金额totalAmount + 税 + 运费 == 完事


    @RequestMapping(value = "/list", name = "分页查看发票")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        InvoiceExample example = new InvoiceExample();
        InvoiceExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        PageInfo pageInfo = invoiceService.findAll(example, page, size);
//        System.out.println(pageInfo);//测试
        request.setAttribute("page", pageInfo);
        return "cargo/invoice/invoice-list";
    }

    @RequestMapping(value = "/toAdd", name = "跳转添加发票")
    public String toAdd(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {

        ShippingOrderExample example = new ShippingOrderExample();
        ShippingOrderExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andStateEqualTo(1);
        PageInfo pageInfo = shippingOrderService.findAll(example, page, size);
        request.setAttribute("page",pageInfo);

        return "cargo/invoice/invoice-add";
    }

    @RequestMapping(value = "/Add", name = "添加发票")
    public String Add(Invoice invoice) {

        System.out.println(invoice);//测试

        invoice.setState(0);//新建状态为 0 草稿
        invoice.setCreateBy(loginUser.getUserName());//创建人 登陆人
        invoice.setCreateDept(loginUser.getDeptName());//创建部门
        Date date = new Date();
        invoice.setCreateTime(date);//创建时间

        Double money = invoiceService.findByMoney(invoice.getInvoiceId(), date);
        invoice.setMoney(money);//金额  各种查询

        invoice.setCompanyId(companyId);
        invoice.setCompanyName(companyName);
        //保存
        invoiceService.save(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转修改发票")
    public String toUpdate(String id, String index) {
        Invoice invoice = invoiceService.findById(id);
//        System.out.println(invoice);//测试
        request.setAttribute("index", index);
        request.setAttribute("invoice", invoice);
        return "cargo/invoice/invoice-update";
    }

    @RequestMapping(value = "/edit", name = "保存修改发票")
    public String edit(Invoice invoice) {
//        System.out.println(invoice);//测试
        invoice.setCompanyId(companyId);
        invoice.setCompanyName(companyName);
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "delete", name = "删除发票")
    public String delete(String id) {
        invoiceService.delete(id);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "cancel", name = "取消提交")
    public String cancel(String id) {
        Invoice invoice = new Invoice();
        invoice.setInvoiceId(id);
        invoice.setState(0);
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "submit", name = "提交发票")
    public String submit(String id) {
        Invoice invoice = new Invoice();
        invoice.setInvoiceId(id);
        invoice.setState(1);
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "invoiceDown", name = "下载发票")
    public void invoiceDown(String id) throws IOException {
        //1.查询数据
        Invoice invoice = invoiceService.findById(id);
        //2.转换数据打印
        //System.out.println(ContractProductVoList);
        //2.1 工作薄
        Workbook wb = new XSSFWorkbook();
        //2.2 创建表
        Sheet sheet = wb.createSheet();
        //2.3 创建excel的大标题
        Row row = sheet.createRow(0);
        //单元格合并Merge 合并   Region 区域范围
        //CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 8));
        Cell cell = row.createCell(1);

        CellStyle cellStyle = bigTitle(wb);//样式
        cell.setCellStyle(cellStyle); //设置样式
        sheet.setDefaultRowHeightInPoints(30);//设置所有的行高

        //2.4 创建excel的小标题
        //2.4.1 准备标题的文字
        String[] headerNames = {"", "发票单号", "scNo", "blNo", "贸易条款", "状态", "创建人", "创建部门", "创建时间", "金额", "企业id", "企业mane"};

        //小标题
        row = sheet.createRow(1);
        //设置cell的宽度
        sheet.setColumnWidth(1, 10 * 256);
        sheet.setColumnWidth(2, 10 * 256);
        sheet.setColumnWidth(3, 10 * 256);
        sheet.setColumnWidth(4, 10 * 256);
        sheet.setColumnWidth(5, 10 * 256);
        sheet.setColumnWidth(6, 10 * 256);
        sheet.setColumnWidth(7, 10 * 256);
        sheet.setColumnWidth(8, 20 * 256);
        sheet.setColumnWidth(9, 10 * 256);
        sheet.setColumnWidth(10, 10 * 256);
        sheet.setColumnWidth(11, 26 * 256);

        //循环创建小标题
        for (int i = 1; i < headerNames.length; i++) {
            cell = row.createCell(i);//创建每一个单元格
            cell.setCellValue(headerNames[i]); //设置值
            cell.setCellStyle(title(wb));//设置样式
        }

        //2.5 构建数据  一行数据一个对象
        int index = 2;

        row = sheet.createRow(index);//创建每一行
        //"id"
        cell = row.createCell(1);
        cell.setCellValue(invoice.getInvoiceId());
        cell.setCellStyle(text(wb));
        //"scNo"
        cell = row.createCell(2);
        cell.setCellValue(invoice.getScNo());
        cell.setCellStyle(text(wb));
        // "blNo"
        cell = row.createCell(3);
        cell.setCellValue(invoice.getBlNo());
        cell.setCellStyle(text(wb));
        // "贸易条款"
        cell = row.createCell(4);
        cell.setCellValue(invoice.getTradeTerms());
        cell.setCellStyle(text(wb));
        // "状态"
        cell = row.createCell(5);
//        Integer i = invoice.getState();
        cell.setCellValue("已提交");
        cell.setCellStyle(text(wb));
        // "创建人"
        cell = row.createCell(6);
        cell.setCellValue(invoice.getCreateBy());
        cell.setCellStyle(text(wb));
        // "创建部门"
        cell = row.createCell(7);
        cell.setCellValue(invoice.getCreateDept());
        cell.setCellStyle(text(wb));
        // "创建时间"
        cell = row.createCell(8);
        Date date = invoice.getCreateTime();
        String Time = date.toString();//转换字符串
        cell.setCellValue(Time);
        cell.setCellStyle(text(wb));
        // "金额"
        cell = row.createCell(9);
        cell.setCellValue(invoice.getMoney());
        cell.setCellStyle(text(wb));
        // "企业id"
        cell = row.createCell(10);
        cell.setCellValue(invoice.getCompanyId());
        cell.setCellStyle(text(wb));
        // "企业name"
        cell = row.createCell(11);
        cell.setCellValue(invoice.getCompanyName());
        cell.setCellStyle(text(wb));


        //3.下载 - response
        DownloadUtil downloadUtil = new DownloadUtil();
        //创建输出流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //将工作薄转换成字节流
        wb.write(byteArrayOutputStream);
        downloadUtil.download(byteArrayOutputStream, response, "发票单.xlsx");

    }

    //int firstRow, int lastRow, int firstCol, int lastCol
    //sheet.addMergedRegion(new CellRangeAddress(0 , 0 , 1 , 8));
    //大标题的样式
    public CellStyle bigTitle(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);                //横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        style.setBorderTop(BorderStyle.THIN);                        //上细线
        style.setBorderBottom(BorderStyle.THIN);                    //下细线
        style.setBorderLeft(BorderStyle.THIN);                        //左细线
        style.setBorderRight(BorderStyle.THIN);                        //右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short) 10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);                //横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);        //纵向居中
        style.setBorderTop(BorderStyle.THIN);                        //上细线
        style.setBorderBottom(BorderStyle.THIN);                    //下细线
        style.setBorderLeft(BorderStyle.THIN);                        //左细线
        style.setBorderRight(BorderStyle.THIN);                        //右细线

        return style;
    }


}

